package com.degratnik.plitki.sql.data;

import java.util.ArrayList;
import java.util.List;

import com.degratnik.plitki.sql.Helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DataSource {

	// Database fields
	private SQLiteDatabase database;
	private Helper dbHelper;
	private String[] allColumns = { Helper.ID, Helper.TITLE, Helper.DETAILS,
			Helper.IMAGE, Helper.DATE, Helper.REMINDER, Helper.PRIORITY };
	private static final int ID = 0;
	private static final int TITLE = 1;
	private static final int DETAILS = 2;
	private static final int IMAGE = 3;
	private static final int DATE = 4;
	private static final int REMINDER = 5;
	private static final int PRIORITY = 6;

	public DataSource(Context context) {
		dbHelper = new Helper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Event createEvent(String event, String details, String image,
			long date, long reminder, long priority) {
		ContentValues values = new ContentValues();
		values.put(Helper.TITLE, event);
		values.put(Helper.DETAILS, details);
		values.put(Helper.IMAGE, image);
		values.put(Helper.DATE, date);
		values.put(Helper.REMINDER, reminder);
		values.put(Helper.PRIORITY, priority);

		long insertId = database.insert(Helper.TABLE_EVENTS, null, values);
		Cursor cursor = database.query(Helper.TABLE_EVENTS, allColumns,
				Helper.ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		Event newEvent = cursorToEvent(cursor);
		cursor.close();
		return newEvent;
	}

	public void deleteEvent(Event event) {
		long id = event.id;
		System.out.println("Comment deleted with id: " + id);
		database.delete(Helper.TABLE_EVENTS, Helper.ID + " = " + id, null);
	}

	public void updateEvent(Event event) {
		long id = event.id;
		System.out.println("Comment updated with id: " + id);
		ContentValues values = new ContentValues();
		values.put(Helper.TITLE, event.getTitle());
		values.put(Helper.DETAILS, event.getDetails());
		values.put(Helper.IMAGE, event.getImage());
		values.put(Helper.DATE, event.date);
		values.put(Helper.REMINDER, event.reminder);
		values.put(Helper.PRIORITY, event.priority);
		database.update(Helper.TABLE_EVENTS, values, Helper.ID + " = " + id,
				null);
	}

	public List<Event> getAllEvents() {
		List<Event> events = new ArrayList<Event>();

		Cursor cursor = database.query(Helper.TABLE_EVENTS, allColumns, null,
				null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Event event = cursorToEvent(cursor);
			events.add(event);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return events;
	}

	private Event cursorToEvent(Cursor cursor) {
		Event event = new Event();
		event.id = cursor.getLong(ID);
		event.setTitle(cursor.getString(TITLE));
		event.setDetails(cursor.getString(DETAILS));
		event.setImage(cursor.getString(IMAGE));
		event.date = cursor.getLong(DATE);
		event.reminder = cursor.getLong(REMINDER);
		event.priority = cursor.getLong(PRIORITY);
		return event;
	}
}
